package com.woniuxy.ams.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniuxy.ams.entity.AssessItem;
import com.woniuxy.ams.entity.CompanyInspectList;
import com.woniuxy.ams.entity.Dept;
import com.woniuxy.ams.entity.ResultEntity;
import com.woniuxy.ams.service.ICompanyInspectListService;
import com.woniuxy.ams.service.ICompanyScoringService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/companyInspectScoring")
public class CompanyScoringController {

    /**
     * 注入service
     */
    @Resource
    ICompanyScoringService companyScoringServiceImpl;
    @Resource
    ICompanyInspectListService companyInspectListServiceImpl;

    /**
     * 考核列表评分
     * @param scoreModels
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping("/score")
    public ResultEntity score(@RequestBody Map<String, Object> scoreModels) throws JsonProcessingException {
        //json转换类，并拿到部门和考核项和考核列表的json字符串
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonDeptStr = objectMapper.writeValueAsString(scoreModels.get("dept"));
        String jsonAssessItemStr = objectMapper.writeValueAsString(scoreModels.get("assessItem"));
        String jsoncompanyInspectListStr = objectMapper.writeValueAsString(scoreModels.get("companyInspectList"));
        //反序列拿到部门列表，考核项列表，公司考核列表对象
        List<Dept> depts = objectMapper.readValue(jsonDeptStr, List.class);
        List<AssessItem> assessItems = objectMapper.readValue(jsonAssessItemStr, List.class);
        CompanyInspectList companyInspectList = objectMapper.readValue(jsoncompanyInspectListStr, CompanyInspectList.class);
        //调用考核评分service的评分方法
        int rows = companyScoringServiceImpl.addScoringData(companyInspectList, assessItems, depts);
        if (rows != 0){
            //修改考核列表评分状态
            int rows2 = companyInspectListServiceImpl.updateScoreStatus("已评分", companyInspectList.getId());
            if (rows2 != 0) {
                return new ResultEntity(200, "评分成功", null);
            }else {
                return new ResultEntity(400, "评分失败", null);
            }
        }else {
            return new ResultEntity(400, "评分失败", null);
        }

    }

}
